package com.example.myapplication.base.mvvm.ui;

import com.example.myapplication.base.mvvm.BaseViewModel;
import com.example.myapplication.base.mvp.IBaseUiView;
import com.harmonyos.livedata.Observer;
import com.example.myapplication.utils.Toast;
import com.harmonyos.viewmodel.ViewModelProvider;
import com.harmonyos.viewmodel.ViewModelStore;
import com.harmonyos.viewmodel.ViewModelStoreOwner;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.lxj.xpopup.interfaces.XPopupCallback;
import com.lxj.xpopup.util.LogUtil;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

public abstract class BaseViewModelFraction<VM extends BaseViewModel> extends Fraction implements IBaseUiView, ViewModelStoreOwner {
    protected VM mViewModel;
    protected Component mComponentView;

    public abstract int getUIContent();

    protected abstract Class<VM> getViewModelClass();

    public abstract void initComponent();

    public void getData() {
    }
    public void initClickListener(){}
    public Context mContext;

    private ViewModelStore viewModelStore;

    private LoadingPopupView loadingPopup;

    @Override
    public ViewModelStore getViewModelStore() {
        if (viewModelStore == null) {
            viewModelStore = new ViewModelStore();
        }
        return viewModelStore;

    }

    public void initViewObservable() {

    }

    private void initUiChangeEvent() {
        mViewModel.getUIChangeLiveDataEvent().getShowLoadingNowEvent()
                .observe(this, new Observer<Boolean>() {
                    @Override
                    public void onChanged(Boolean aBoolean) {
                        if (aBoolean) {
                            showLoadingDialogNow(true);
                        } else {
                            showLoadingDialog();
                        }
                    }
                });
        mViewModel.getUIChangeLiveDataEvent().getDismissLoadingDialogEvent()
                .observe(this, new Observer<Boolean>() {
                    @Override
                    public void onChanged(Boolean aBoolean) {
                        dismissLoadingDialog();
                    }
                });
        mViewModel.getUIChangeLiveDataEvent().getShowToastMessageEvent()
                .observe(this, new Observer<String>() {
                    @Override
                    public void onChanged(String s) {
                        showToast(s);
                    }
                });
        mViewModel.getUIChangeLiveDataEvent().getShowEmptyViewEvent()
                .observe(this, new Observer<Boolean>() {
                    @Override
                    public void onChanged(Boolean aBoolean) {
                        showEmptyView();
                    }
                });
        mViewModel.getUIChangeLiveDataEvent().getShowNetworkErrorEvent()
                .observe(this, new Observer<Boolean>() {
                    @Override
                    public void onChanged(Boolean aBoolean) {
                        showErrorView();
                    }
                });

    }

    private void initLoadindDialog() {
        loadingPopup = (LoadingPopupView) new XPopup.Builder(getContext())
                .dismissOnBackPressed(true)
                .dismissOnTouchOutside(false)
                .setPopupCallback(new XPopupCallback() {
                    @Override
                    public void onCreated(BasePopupView basePopupView) {

                    }

                    @Override
                    public void beforeShow(BasePopupView basePopupView) {

                    }

                    @Override
                    public void onShow(BasePopupView basePopupView) {

                    }

                    @Override
                    public void onDismiss(BasePopupView basePopupView) {

                    }

                    @Override
                    public void beforeDismiss(BasePopupView basePopupView) {

                    }

                    @Override
                    public boolean onBackPressed(BasePopupView basePopupView) {
                        LogUtil.error("htd", "取消");
                        return false;
                    }

                    @Override
                    public void onKeyBoardStateChanged(BasePopupView basePopupView, int i) {

                    }

                    @Override
                    public void onDrag(BasePopupView basePopupView, int i, float v, boolean b) {

                    }
                })
                .asLoading("加载中");


    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        mComponentView = scatter.parse(getUIContent(), container, false);
        mContext = getFractionAbility();

        initLoadindDialog();
        mViewModel = new ViewModelProvider(getViewModelStore(), ViewModelProvider.HarmonyViewModelFactory.getInstance(getApplicationContext())).get(getViewModelClass());
        initUiChangeEvent();
        initViewObservable();
        initComponent();
        initClickListener();
        getData();
        return mComponentView;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onStop() {
        super.onStop();
        getViewModelStore().clear();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    public String getString(int resId) {
        try {
            return getFractionAbility().getResourceManager().getElement(resId).getString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public int getColor(int colorId) {
        try {
            return getFractionAbility().getResourceManager().getElement(colorId).getColor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void showLoadingDialog() {
        if (loadingPopup != null && !loadingPopup.isShow()) {
            loadingPopup.show();
        }
    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showErrorMsg(int errorCode, String msg) {
        showToast(msg);
    }

    @Override
    public void showToast(String message) {
        Toast.makeToast(mContext, message, 3000).show();
    }


    @Override
    public void showLoadingDialogNow(boolean isNow) {
        if (isNow) {
            showLoadingDialog();
        } else {
            if (loadingPopup != null && !loadingPopup.isShow()) {
                getFractionAbility().postTask(() -> showLoadingDialog(), 750);
            }
        }

    }

    @Override
    public void dismissLoadingDialog() {
        if (loadingPopup != null) loadingPopup.dismiss();
    }

    @Override
    public void showErrorView() {

    }

    @Override
    public void showNoMoreView() {

    }
}
